<?php


namespace app\api\controller;
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");


use app\api\Api;
use app\api\controller\Jwt as Jwt;
use think\Db;
use think\Request;

/**
 * Class User
 * @package app\api\controller
 */
class User extends Api
{
    //初始化操作,验证token
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub

    }
    /**
     * dev.ziwudiche.com/api/user/mobilelogin
     * 手机验证码登录
     *
     * @param string $mobile 手机号
     * @param string $captcha 验证码
     */
    public function mobilelogin()
    {
        $mobile = input('mobile');
        $captcha = input('captcha');
        $code = Db::name('verification_code')->where('tel', $mobile)->find();

        $time = time() - $code["ctime"];
        if ($captcha != $code["code"]) {
            echo json_encode(["code" => 301, "msg" => '验证码错误'], JSON_UNESCAPED_UNICODE);die();
        } elseif ($time > $code["effective_time"]) {
            echo json_encode(["code" => 302, "msg" => '验证码过期'], JSON_UNESCAPED_UNICODE);die();
        } else {
            $result = Db::name('user')->where('tel', $mobile)->find();
            if (!$result) {
                $user_data = [
                    'tel' => $mobile,
                    'ctime' => date('Y-m-d H:i:s', time()),
                ];
                $uid=Db::name('user')->insertGetId($user_data);
                $token=Jwt::getToken($uid,strtotime('+15 days'));
                $this->return_arr["data"]=[
                    "token" => $token,
                ];
                $this->return_arr["msg"]= '登录成功';
            } else {

                $token=Jwt::getToken($result['id'],strtotime('+15 days'));
                $this->return_arr["data"]=[
                    "token" => $token,
                ];
                $this->return_arr["msg"]= '登录成功';
            }
            $this->echoResult($this->return_arr);

        }
    }

    /**
     * 手机号  密码登录
     *dev.ziwudiche.com/api/user/login
     * @param string $mobile 手机号
     * @param string $password 密码
     */
    public function login()
    {
        $mobile = input('mobile');
        $password = md5(input('password'));
        $result = Db::name('user')
            ->where('tel', $mobile)
            ->where('pwd', $password)
            ->find();
        if ($result) {
            $token=Jwt::getToken($result['id'],strtotime('+15 days'));

            $this->return_arr["data"]=[
                "token" => $token,
            ];
            $this->return_arr["msg"]= '登录成功';
        } else {
            $this->return_arr["data"]=[
                "token" => ' ',
            ];
            $this->return_arr["msg"]= '登录失败';
        }
        $this->echoResult($this->return_arr);

    }

    /**
     * 手机号  密码登录
     *dev.ziwudiche.com/api/user/login
     * @param string $mobile 手机号
     * @param string $password 密码
     */
    public function register()
    {
        $mobile = input('mobile');
        $password = md5(input('password'));
        $name=input('name');
        $id_number=input('id_number');
        $type=input('type');
        if ($type!=1){
            $company=input('company');
        }
//        $data=[
//
//        ]

        $result = Db::name('user')
            ->where('tel', $mobile)
            ->where('pwd', $password)
            ->find();
        if ($result) {
            $token=Jwt::getToken($result['id'],strtotime('+15 days'));

            $this->return_arr["data"]=[
                "token" => $token,
            ];
            $this->return_arr["msg"]= '登录成功';
        } else {
            $this->return_arr["data"]=[
                "token" => ' ',
            ];
            $this->return_arr["msg"]= '登录失败';
        }
        $this->echoResult($this->return_arr);

    }

    /**
     * 重置密码
     *dev.fastadmin.com/api/user/resetpwd
     * @param string $mobile 手机号
     * @param string $captcha 验证码
     * @param string $password 密码
     */
    public function  resetpwd()
    {
        $mobile = input('mobile', 18655992737);
        $captcha = input('captcha');
        $password = input('password');

        $code = Db::name('verification_code')->where('tel', $mobile)->find();
        $time = time() - $code["ctime"];
        if ($captcha != $code["code"]) {
            echo json_encode(["code" => 301, "msg" => '验证码错误'], JSON_UNESCAPED_UNICODE);die();
        } elseif ($time > $code["effective_time"]) {
            echo json_encode(["code" => 302, "msg" => '验证码过期'], JSON_UNESCAPED_UNICODE);die();
        } else {
            $res = Db::name('user')->where('tel', $mobile)->update(['pwd' => md5($password)]);
            if ($res) {
                echo json_encode(["code" => 200, "msg" => '设置成功'], JSON_UNESCAPED_UNICODE);die();
            } else {
                echo json_encode(["code" => 400, "msg" => '设置失败'], JSON_UNESCAPED_UNICODE);die();
            }
        }
    }


    /**
     * 注销登录
     * dev.ziwudiche.com/api/user/logout
     */
    public function logout()
    {
        $token = Request::instance()->header('Authorization');
        $result=Jwt::verifyToken($token);
        if ($result){
            echo json_encode(["code" => 200, "msg" => '注销成功'], JSON_UNESCAPED_UNICODE);die();
        }else{
            echo json_encode(["code" => 400102, "msg" => '请先登录'], JSON_UNESCAPED_UNICODE);die();
        }
    }

}